package com.lethelh.reflect;

import com.lethelh.reflect.entity.Book;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 反射: Book测试类
 */
public class BookSample {
    public static void main(String[] args) {
        try {
            Class bookClass = Class.forName("com.lethelh.reflect.entity.Book");
            Constructor bookConstructor = bookClass.getConstructor(new Class[]{
                    Integer.class, String.class, Float.class
            });
            Book book = (Book)bookConstructor.newInstance(new Object[]{
               66, "《海底两万里》", 56f
            });
            System.out.println(book);

            Field[] fields = bookClass.getDeclaredFields();
            for (Field field:fields) {
                if (field.getModifiers() == 1){// 1 为public
                    System.out.println("public:"+field.getName());
                }else if (field.getModifiers() == 2){// 2 为private
                    System.out.println("private:"+field.getName());
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
